/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* FlexPopupMenu.java
*
* Created on December 25, 2007, 5:17 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package javautil.swing.components;

import javautil.collections.SetUtil;

import java.awt.Component;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author stewari1
 */
public class FlexPopupMenu extends JPopupMenu {

    /** Field description */
    private final Map<Action, JMenuItem> actionMenuItemMap = new Hashtable<>();

    /** Creates a new instance of FlexPopupMenu */
    public FlexPopupMenu() {}

    public JMenuItem add(Action action) {

        JMenuItem jMenuItem = super.add(action);

        actionMenuItemMap.put(action, jMenuItem);

        return jMenuItem;
    }

    public void addAll(Set<Action> actions) {

        for (Action action : actions) {

            actionMenuItemMap.put(action, super.add(action));
        }
    }

    public void removeAll(Set<Action> actions) {

        for (Action action : actions) {

            remove(actionMenuItemMap.get(action));
            actionMenuItemMap.remove(action);
        }
    }
}
